import fetch from 'utils/fetch';

// 注册
export function register(data) {
  return fetch({
    url: '/accounts/signup/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 检测是否关注公众号
export function checkFocus() {
  return fetch({
    url: '/user/check_sub/',
    method: 'get'
  })
}

// 登录
export function login(phone, password) {
  const data = {
    phone,
    password
  };
  return fetch({
    url: '/accounts/login/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
}

// 注销
export function logout() {
  return fetch({
    url: '/accounts/logout/',
    method: 'get'
  });
}

// 获取用户信息
export function getInfo() {
  return fetch({
    url: '/user/info/',
    method: 'get'
  });
}

// 验证密码手机号
export function checkInfo(data) {
  return fetch({
    url: '/user/check/info/',
    method: 'get',
    params: data
  })
}

// 重置新密码
export function resetPwd(data) {
  return fetch({
    url: '/user/reset/passwd/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 更改新密码
export function modifyPwd(data) {
  return fetch({
    url: '/user/modify/passwd/',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}

// 更改手机号
export function changePhone(data) {
  return fetch({
    url: '/user/update/phone/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 编辑用户信息
export function editUser(data) {
  return fetch({
    url: '/user/edit/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      const form = new FormData()
      for (const key in data) {
        form.append(key, data[key])
      }
      return form
    }],
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取钱包
export function getWallet() {
  return fetch({
    url: '/user/wallet/',
    method: 'get'
  })
}

// 获取交易记录--收支明细
export function getDealRecord(data) {
  return fetch({
    url: '/user/dealrecord/',
    method: 'get',
    params: data
  })
}

// 发布语点交易
export function giveTrade(data) {
  return fetch({
    url: '/user/publish/trade/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 语点交易列表
export function getTrade(data) {
  return fetch({
    url: '/user/list/trade/',
    method: 'get',
    params: data
  })
}

// 购买语点交易
export function buyTrade(data) {
  return fetch({
    url: '/user/buy/',
    method: 'get',
    params: data
  })
}

// 获取账户信息
export function getMyAccount(data) {
  return fetch({
    url: '/user/accounts/',
    method: 'get',
    params: data
  })
}

export function addAccount(data) {
  return fetch({
    url: '/user/add/acc/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

export function getCash(data) {
  return fetch({
    url: '/user/cash/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 获取课程
export function getCourse(data, method) {
  if (method === 'post') {
    return fetch({
      url: '/user/course/',
      method,
      data,
      transformRequest: [function(data) {
        // Do whatever you want to transform the data
        const form = new FormData()
        for (const key in data) {
          form.append(key, data[key])
        }
        return form
      }],
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  } else if (method === 'delete') {
    return fetch({
      url: '/user/course/',
      method,
      params: data
    })
  } else {
    return fetch({
      url: '/user/course/',
      method: 'get',
      params: data
    })
  }
}

// 我的播单、我的历史、我的收藏(api/common)、我的下载、我的上传
// 获取我的播单
export function getPlayList(data) {
  return fetch({
    url: '/user/playlist/',
    method: 'get',
    params: data
  })
}

// 获取我的浏览历史
export function getHistory(data) {
  return fetch({
    url: '/user/history/',
    method: 'get',
    params: data
  })
}

// 获取我的上传
export function getUpload(data) {
  return fetch({
    url: 'user/upload/',
    method: 'get',
    params: data
  })
}

// 获取我的下载
export function getDownload(data) {
  return fetch({
    url: '/user/dlrecord/',
    method: 'get',
    params: data
  })
}

// 更新工作经历
export function updateJob(data, method) {
  if (method === 'post') {
    return fetch({
      url: '/user/update/job/',
      method,
      headers: {
        'Content-Type': 'application/json'
      },
      data
    })
  } else {
    return fetch({
      url: '/user/update/job/',
      method: 'get',
      params: data
    })
  }
}

// 更新教学风格
export function updateStyle(data) {
  return fetch({
    url: '/user/update/style/',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}

// 订单
export function getOrder(data) {
  return fetch({
    url: '/user/order/',
    method: 'get',
    params: data
  })
}

// 同城优教信息：获取和修改
export function getMyOrg(data, method) {
  if (method === 'post') {
    return fetch({
      url: '/org/edit/',
      method,
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      data,
      transformRequest: [function(data) {
        // Do whatever you want to transform the data
        const form = new FormData()
        for (const key in data) {
          form.append(key, data[key])
        }
        return form
      }]
    })
  } else {
    return fetch({
      url: '/org/edit/',
      method: 'get'
    })
  }
}

// 申请首页广告
export function applyAdv(data) {
  return fetch({
    url: '/org/apply/ads/',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  })
}

// 机构老师
export function updateTeacher(data, method) {
  if (method === 'post') {
    return fetch({
      url: '/org/teacher/',
      method: 'post',
      headers: {
        'Content-Type': 'application/json'
      },
      data
    })
  } else {
    return fetch({
      url: '/org/teacher/',
      method
    })
  }
}